import {Injectable} from '@angular/core';
import {TagMap} from '../entity/tag-map.model';
import {AppConfig} from '../config';
import {MatrixRelation} from '../entity/matrix-relation.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class TagMapService {
  private readonly param = 'tagMaps';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  getTagMaps(): Promise<TagMap[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as TagMap[])
      .catch(this.handleError);
  }

  /**
   * 根据TagId获得所有型号下的TagMap
   *
   * @param {number} tagId
   * @returns {Promise<TagMap[]>}
   * @memberof TagMapService
   */
  getTagMapsByTagId(tagId: number): Promise<TagMap[]> {
    return this.http.get(`${this.url}/tagId/${tagId}`)
      .toPromise()
      .then(response => response.data as TagMap[])
      .catch(this.handleError);
  }

  /**
   * 根据TagId获得矩阵映射关系
   *
   * @param {number} tagId
   * @returns {Promise<MatrixRelation[]>}
   * @memberof TagMapService
   */
  getMatrixRelationByTagId(tagId: number): Promise<MatrixRelation[]> {
    return this.http.get(`${this.url}/matrixRelation/${tagId}`)
      .toPromise()
      .then(response => response.data as MatrixRelation[])
      .catch(this.handleError);
    // return Promise.resolve([{
    //   tagId: 632,
    //   modelId: 1,
    //   logicGroupId: 1,
    //   sateGroupId: 1,
    // }, {
    //   tagId: 632,
    //   modelId: 4,
    //   logicGroupId: 1,
    //   sateGroupId: 1,
    // }, {
    //   tagId: 632,
    //   modelId: 3,
    //   logicGroupId: 1,
    //   sateGroupId: 1,
    // }, {
    //   tagId: 632,
    //   modelId: 2,
    //   logicGroupId: 1,
    //   sateGroupId: 1,
    // }
    // ]);

  }

  getFaultsBySchemeId(schemeId: number): Promise<TagMap[]> {
    return this.http.get(`${this.url}/schemeId/${schemeId}`)
      .toPromise()
      .then(response => response.data as TagMap[])
      .catch(this.handleError);
  }

  create(tagMap: TagMap): Promise<boolean> {
    // const apiUrl = `http://192.168.15.150:8585/webapi/v1/tagMaps`;
    return this.http.post(this.url, JSON.stringify(tagMap))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  update(tagMap: TagMap): Promise<TagMap> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(tagMap))
      .toPromise()
      .then(() => tagMap)
      .catch(this.handleError);
  }

  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  batchDelete(valueIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}


// WEBPACK FOOTER //
// ./src/app/service/tag-map.service.ts
